package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.EnterpriseQualification;
import com.innovations.inn.service.IEnterpriseQualificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业资质Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "企业资质")
@RestController
@RequestMapping("/inn/qualification")
public class EnterpriseQualificationController extends BaseController {
    @Autowired
    private IEnterpriseQualificationService enterpriseQualificationService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询企业资质列表
     */
    @ApiOperation("查询企业资质列表")
    @PreAuthorize("@ss.hasPermi('inn:qualification:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterpriseQualification enterpriseQualification) {
        startPage();
        List<EnterpriseQualification> list = enterpriseQualificationService.selectEnterpriseQualificationList(enterpriseQualification);
        return getDataTable(list);
    }

    /**
     * 导出企业资质列表
     */
    @PreAuthorize("@ss.hasPermi('inn:qualification:export')")
    @Log(title = "企业资质", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterpriseQualification enterpriseQualification) {
        List<EnterpriseQualification> list = enterpriseQualificationService.selectEnterpriseQualificationList(enterpriseQualification);
        ExcelUtil<EnterpriseQualification> util = new ExcelUtil<EnterpriseQualification>(EnterpriseQualification.class);
        util.exportExcel(response, list, "企业资质数据");
    }

    /**
     * 获取企业资质详细信息
     */
    @ApiOperation("获取企业资质详细信息")
    @PreAuthorize("@ss.hasPermi('inn:qualification:query')")
    @GetMapping(value = "/{enterpriseQualificationId}")
    public AjaxResult getInfo(@PathVariable("enterpriseQualificationId") Long enterpriseQualificationId) {
        return success(enterpriseQualificationService.selectEnterpriseQualificationByEnterpriseQualificationId(enterpriseQualificationId));
    }

    /**
     * 新增企业资质
     */
    @ApiOperation("新增企业资质")
    @PreAuthorize("@ss.hasPermi('inn:qualification:add')")
    @Log(title = "企业资质", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody EnterpriseQualification enterpriseQualification) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseQualificationService.completeData(enterpriseQualification, userId, 1);
        return toAjax(enterpriseQualificationService.insertEnterpriseQualification(enterpriseQualification));
    }

    /**
     * 修改企业资质
     */
    @ApiOperation("修改企业资质")
    @PreAuthorize("@ss.hasPermi('inn:qualification:edit')")
    @Log(title = "企业资质", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody EnterpriseQualification enterpriseQualification) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        enterpriseQualificationService.completeData(enterpriseQualification, userId, 2);
        return toAjax(enterpriseQualificationService.updateEnterpriseQualification(enterpriseQualification));
    }

    /**
     * 删除企业资质
     */
    @ApiOperation("删除企业资质")
    @PreAuthorize("@ss.hasPermi('inn:qualification:remove')")
    @Log(title = "企业资质", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enterpriseQualificationIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseQualificationIds) {
        return toAjax(enterpriseQualificationService.deleteEnterpriseQualificationByEnterpriseQualificationIds(enterpriseQualificationIds));
    }
}
